package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DbusinessDao;
import cn.gson.fengboot.model.dao.TfyclientFormDao;
import cn.gson.fengboot.model.dao.ZcontractDao;
import cn.gson.fengboot.model.dao.ZcontractdetailsDao;
import cn.gson.fengboot.model.mapper.DofferdetailsMapper;
import cn.gson.fengboot.model.mapper.ZcontractMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZcontractService {
    @Autowired
    ZcontractMapper ddmapper;

    @Autowired
    DofferdetailsMapper bjmapper;

    @Autowired
    ZcontractDao dao;

    @Autowired
    ZcontractdetailsDao xqdao;

    @Autowired
    DbusinessDao sjdao;

    @Autowired
    TfyclientFormDao khdao;

//    根据商机ID查询对应的报价详情信息
    public List<DofferdetailsHt> allpro(Integer sjid){
        System.out.println(sjid);
        return bjmapper.allpro(sjid);
    }

    /*
        合同弹框查询方法
     */
    public List<Zcontract> hetonlist(String search, String search2){
        return ddmapper.hetonlist(search,search2);
    }

    /*
        合同列表查询方法
    */
    public List<Zcontract> hetonlist2(String search, String search2){
        return ddmapper.hetonlist2(search,search2);
    }

    /*
        订单页面查询方法
    */
    public List<Zorder> ddlist2(String search,String search2){
        return ddmapper.ddlist2(search,search2);
    }


    /*根据合同ID查询合同详情所有产品信息*/
    public List<Zcontractdetails> allpro(String search){
        return ddmapper.allpro(search);
    }

    /*新增修改合同*/
    public void add(Zcontract ht){
        Zcontract save = dao.save(ht);
            ht.getZcontractdetailsByConId().forEach(pur->{
                pur.setConId(save);
            });
            xqdao.saveAll(ht.getZcontractdetailsByConId());
            //修改商机阶段为签单
            Dbusiness dbusiness = sjdao.findById(ht.getBusId().getBusId()).get();
            Dphase jd = new Dphase();
            jd.setPhaseId(6);
            dbusiness.setPhase(jd);
            sjdao.save(dbusiness);
            //修改客户为签约客户
            TfyclientForm tfyclientForm = khdao.findById(ht.getClientId().getClientId()).get();
            Integer ht1 = ddmapper.ht(ht.getClientId().getClientId());
            if(ht1>=1){
                tfyclientForm.setClientKhzt("重复购买");
            }else {
                tfyclientForm.setClientKhzt("签约客户");
            }
        khdao.save(tfyclientForm);
    }

    //<!--谭方圆=>客户合并的合同查询-->
    public List<Zcontract> tfyhetonlist(Integer id1, Integer id2){
        return ddmapper.tfyhetonlist(id1,id2);
    }

}
